package com.miracle.login.module.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.miracle.basemvp.base.BaseActivity;
import com.miracle.basemvp.di.component.AppComponent;
import com.miracle.common.util.PreferencesUtils;
import com.miracle.common.util.S;
import com.miracle.login.R;
import com.miracle.login.R2;
import com.miracle.login.module.di.component.DaggerLoginComponent;
import com.miracle.login.module.di.module.LoginModule;
import com.miracle.login.module.mvp.contract.LoginContract;
import com.miracle.login.module.mvp.entity.request.LoginRequest;
import com.miracle.login.module.mvp.presenter.LoginPresenter;
import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.android.ActivityEvent;

import butterknife.BindView;
import me.wangyuwei.particleview.ParticleView;

import static com.miracle.common.util.PreferencesUtils.State.USER_SESSIONID;

/**
 * com.miracle.login.module.mvp.ui.activity
 * (c)2018 AIR Times Inc. All rights reserved.
 *
 *
 * @author WangJQ
 * @version 1.0
 * @date 2018/7/6 17:42
 * @see com.miracle.login.module.mvp.ui.activity
 */
public class SplashActivity extends BaseActivity<LoginPresenter> implements LoginContract.View {
    @BindView(R2.id.pa)
    ParticleView mParticleView;
    @BindView(R2.id.ll_persion)
    LinearLayout mLLPersion;
    @BindView(R2.id.tv_name)
    TextView mTvName;

    LifecycleProvider<ActivityEvent> lifecycleProvider;

    boolean mAnimFinish = false;
    boolean mIsLoginSucc = false;

    @Override
    public int setLayoutId(@Nullable Bundle bundle) {
        return R.layout.activity_splash;
    }

    @Override
    public void initView() {
        mParticleView.startAnim();
        mParticleView.setOnParticleAnimListener(new ParticleView.ParticleAnimListener() {
            @Override
            public void onAnimationEnd() {
                Log.d("WJQ", "onAnimationEnd");
                if (mIsLoginSucc) {
                    startMain();
                } else {
                    startLogin();
                }
                finish();
            }
        });

    }

    @Override
    public void initData() {
        String name = PreferencesUtils.getPreferences(mContext, PreferencesUtils.State.USER_NAME);
        if (TextUtils.isEmpty(name)) {
            mLLPersion.setVisibility(View.GONE);
        } else {
            mTvName.setText("Hi " + name);
        }
    }

    @Override
    public void setDaggerComponent(AppComponent appComponent) {
        DaggerLoginComponent
                .builder()
                .appComponent(appComponent)
                .loginModule(new LoginModule(this))
                .build()
                .inject(this);
    }

    @Override
    public void onRequest(LifecycleProvider<ActivityEvent> lifecycleProvider) {
        this.lifecycleProvider = lifecycleProvider;
        LoginRequest request = new LoginRequest();
        String sessionId = PreferencesUtils.getPreferences(mContext, USER_SESSIONID);
        if (TextUtils.isEmpty(sessionId)) {
            return;
        }
        request.setJSESSIONID(sessionId);
        mPresenter.login(lifecycleProvider, request);
        mPresenter.checkVersion(lifecycleProvider);
    }

    @Override
    public void loginSucc() {
        Log.d("WJQ", "loginSucc");
//        S.Long(mParticleView, "登陆成功").info().show();
        mIsLoginSucc = true;
        if (mAnimFinish) {
            startMain();
        }
    }

    @Override
    public void FindANewVersion(String msg) {

    }

    @Override
    public void NoNewVersion() {

    }

    @Override
    public void showData(Object o) {

    }

    public void startMain() {
        ARouter.getInstance()
                .build("/main/MainActivity")
                .navigation();
    }

    public void startLogin() {
        showActivity(new Intent(mContext, LoginActivity.class));
    }
}
